;
! function() {
	var layer = layui.layer,
		form = layui.form,
		laypage = layui.laypage;

	layer.load(2);

	to_page(1, 10);
	
	// 请求分页数据
	function to_page(pageNum, pageSize) {
		$.ajax({
			url: "/appsystemcontact/",
			type: "post",
			data: {
				"pageNum": pageNum,
				"pageSize": pageSize
			},
			async: false,
			success: function(result) {
				layer.closeAll('loading');
				if(result.totalRow == 0) {
					// 提示没有数据
					$("#content").html("<tr><td colspan='4' style='text-align:center;font-size: 18px;color:#FF6700;'>没有查询到任何数据哦~</td></tr>");
					// 隐藏分页条
					$("#page_info").hide();
				} else {
					// 拼装分页总览信息并赋给页面
					build_pageInfo(result);
					// 拼装tr并赋给页面
					build_table(result);
					// 显示分页按钮
					laypage.render({
						elem: 'page_nav_div',
						count: result.totalRow,
						curr: result.pageNumber,
						limit: result.pageSize,
						theme: "#FF6700",
						prev: "<em>«</em>",
						next: "<em>»</em>",
						layout: ['prev', 'page', 'next', 'limit', 'skip'],
						jump: function(obj, first) {
							if(!first) {
								to_page(obj.curr, obj.limit);
							}
						}
					});
					// 显示分页条
					$("#page_info").show();
				}
			}
		});
	}

	// 构建分页细节
	function build_pageInfo(pageInfo) {
		$("#pagination_overview").html(
			"当前第" + pageInfo.pageNumber + "页," +
			"共有" + pageInfo.totalPage +
			"页,总计" +
			pageInfo.totalRow + "条记录");
	}

	// 构建表格
	function build_table(page) {
		// 清空tbody
		$("#content").empty();
		// 遍历数据并累加
		$.each(page.list, function() {
			var qqTd = $("<td></td>").append(this.qq);
			var emailTd = $("<td></td>").append(this.email);
			var phoneTd = $("<td></td>").append(this.phone);
			var contentTimeTd = $("<td></td>").append(this.content);
			var	operation = "<td>" +
							"<a class='layui-btn layui-btn-radius layui-btn-sm layui-btn-normal' href='/operationAppSystemContact/"+this.id+"'>" +
							"<i class='layui-icon'>&#xe642;</i>编辑</a>"+
							"<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-danger del-contact'>" +
							"<i class='layui-icon'>&#xe640;</i>删除</button>" +
							"</td>";
			// 赋给tbody
			$("<tr id='" + this.id + "'></tr>").append(qqTd).append(emailTd).append(phoneTd).append(contentTimeTd).append(operation).appendTo("#content");
		});
	}

	//删除职业类型
	$(document).on("click", ".del-contact", function() {
		var id = $(this).parent().parent().attr("id");
		layer.confirm("确定要删除吗?", {
			icon: 3,
			title: '温馨提示'
		}, function(index) {
			$.ajax({
				url: "/appsystemcontact/delAppSystemContact",
				type: "post",
				data: {
					"id": id
				},
				success: function(result) {
					layer.msg(result.msg);
					if(result.status == 0) {
						to_page(1, 10);
					}
				}
			});
			layer.close(index);
		});
	});
}();